<?php
defined ( '_JEXEC' ) or die ( 'Restricted access' );
jimport ( 'joomla.application.component.controller' );
class FudbalController extends JController {
	function __construct($default = array()) {
		parent::__construct ( $default );
		$this->registerTask ( 'add', 'editTimes' );
		$this->registerTask ( 'apply', 'saveTimes' );
		$this->registerTask ( 'save', 'saveTimes' );
		$this->registerTask ( 'remove', 'removeTimes' );
		$this->registerTask ( 'edit', 'editTimes' );
		$this->registerTask ( 'kolo', 'prikaziKola' );
		$this->registerTask ( 'primeniIzmeneKola', 'sacuvajKolo' );		
		$this->registerTask ( 'lige', 'prikaziLige' );
		$this->registerTask ( 'primeniIzmeneLige', 'sacuvajLigu' );		
		$this->registerTask ( 'ligaunpublish', 'ligapublish' );
		$this->registerTask ( 'treneri', 'prikaziTrenere' );
		$this->registerTask ( 'primeniIzmeneTrenera', 'sacuvajTrenera' );		
		$this->registerTask ( 'trenerunpublish', 'trenerpublish' );
		$this->registerTask ( 'klubovi', 'prikaziKlubove' );
		$this->registerTask ( 'primeniIzmeneKluba', 'sacuvajKlub' );		
		$this->registerTask ( 'klubunpublish', 'klubpublish' );
		$this->registerTask ( 'motiv_times', 'showMtimes' );
		$this->registerTask ( 'motivp_times', 'showMptimes' );
		$this->registerTask ( 'qf_times', 'showQtimes' );
	}

	function editTimes() {
		global $option;
		$row = & JTable::getInstance ( 'Times', 'Table' );
		$cid = JRequest::getVar ( 'cid', array (0 ), '', 'array' );
		$db = & JFactory::getDBO ();
		$id = $cid [0];
		$row->load ( $id );
		//converting float data time in standard format
		$seconds = floor($row->time);
		$hundreds = ($row->time - $seconds)*100;
		if($hundreds < 10){
			$hundreds = '0'.$hundreds;
		}
		if($seconds < 10){
			$seconds = '0'.$seconds;
		}
		if($seconds >= 60){
			$minutes = floor($seconds / 60);
			$seconds = $seconds % 60;
				
			$row->minutes = $minutes ;
			$row->seconds = $seconds ;
			$row->hundreds = $hundreds ;
		}else{
			$row->minutes = 0 ;
			$row->seconds = $seconds ;
			$row->hundreds = $hundreds ;
		}

		$lists = array ( );

		$query = "SELECT user_id, firstname, middlename, lastname FROM #__comprofiler ORDER BY firstname ASC";
		$db->setQuery ( $query );
		$rowsSwimmers = $db->loadObjectList ();
		for($i = 0; $i < count ( $rowsSwimmers ); $i ++) {
			$rowsSwimmers [$i]->firstname = $rowsSwimmers [$i]->firstname . ' ' . $rowsSwimmers [$i]->middlename . ' ' . $rowsSwimmers [$i]->lastname;
		}

		$query2 = "SELECT id, name FROM #__st_meets ORDER BY name ASC";
		$db->setQuery ( $query2 );
		$rowsMeets = $db->loadObjectList ();

		$query3 = "SELECT id, short_name, long_name FROM #__st_course_type ORDER BY long_name ASC";
		$db->setQuery ( $query3 );
		$rowsCourse = $db->loadObjectList ();
		for($i = 0; $i < count ( $rowsCourse ); $i ++) {
			$rowsCourse [$i]->long_name = $rowsCourse [$i]->long_name . ' (' . $rowsCourse [$i]->short_name . ')';
		}

		$query4 = "SELECT id, long_name FROM #__st_stroke ORDER BY long_name ASC";
		$db->setQuery ( $query4 );
		$rowsStroke = $db->loadObjectList ();

		$query5 = "SELECT id, distance, unit FROM #__st_distance ORDER BY distance ASC";
		$db->setQuery ( $query5 );
		$rowsDistance = $db->loadObjectList ();
		for($i = 0; $i < count ( $rowsDistance ); $i ++) {
			$rowsDistance [$i]->distance = $rowsDistance [$i]->distance . ' ' . $rowsDistance [$i]->unit;
		}

		$query6 = "SELECT id, name FROM #__st_race_type ORDER BY name ASC";
		$db->setQuery ( $query6 );
		$rowsRaceTypes = $db->loadObjectList ();

		$lanes = array(
			'0' => array('value' => '0',
			'text' => 'None'),
			'1' => array('value' => '1',
			'text' => '1'),
			'2' => array('value' => '2',
			'text' => '2'),
			'3' => array('value' => '3',
			'text' => '3'),
			'4' => array('value' => '4',
			'text' => '4'),
			'5' => array('value' => '5',
			'text' => '5'),
			'6' => array('value' => '6',
			'text' => '6'),
			'7' => array('value' => '7',
			'text' => '7'),
			'8' => array('value' => '8',
			'text' => '8'),
		);

		$lists['lanes'] = JHTML::_('select.genericList', $lanes, 'lane', 'class="inputbox" '. '', 'value', 'text', $row->lane );
		$lists ['swimmers'] = JHTML::_ ( 'select.genericList', $rowsSwimmers, 'swimmer_id', 'class="inputbox" ' . '', 'user_id', 'firstname', $row->swimmer_id );
		$lists ['meets'] = JHTML::_ ( 'select.genericList', $rowsMeets, 'meet_id', 'class="inputbox" ' . '', 'id', 'name', $row->meet_id );
		$lists ['courses'] = JHTML::_ ( 'select.genericList', $rowsCourse, 'course_type_id', 'class="inputbox" ' . '', 'id', 'long_name', $row->course_type_id );
		$lists ['strokes'] = JHTML::_ ( 'select.genericList', $rowsStroke, 'stroke_id', 'class="inputbox" ' . '', 'id', 'long_name', $row->stroke_id );
		$lists ['distances'] = JHTML::_ ( 'select.genericList', $rowsDistance, 'distance_id', 'class="inputbox" ' . '', 'id', 'distance', $row->distance_id );
		$lists ['racetypes'] = JHTML::_ ( 'select.genericList', $rowsRaceTypes, 'race_type_id', 'class="inputbox" ' . '', 'id', 'name', $row->race_type_id );

		HTML_reviews::editResult ( $row, $lists, $option );
	}

	function saveTimes() {
		global $option;
		$row = & JTable::getInstance ( 'Times', 'Table' );
		if (! $row->bind ( JRequest::get ( 'post' ) )) {
			echo "<script> alert('" . $row->getError () . "'); window.history.go(-1); </script>\n";
			exit ();
		}
		$row->swimmer_id = JRequest::getInt ( 'swimmer_id', '0', 'post');
		$row->meet_id = JRequest::getInt ( 'meet_id', '0', 'post');
		$row->course_type_id = JRequest::getInt ( 'course_type_id', '0', 'post');
		$row->stroke_id = JRequest::getInt ( 'stroke_id', '0', 'post');
		$row->distance_id = JRequest::getInt ( 'distance_id', '0', 'post');
		$row->race_type_id = JRequest::getInt ( 'race_type_id', '0', 'post');
		$row->rank = JRequest::getInt ( 'rank', '0', 'post');
		$row->lane = JRequest::getInt ( 'lane', '0', 'post');
		$timemin = JRequest::getInt ( 'timemin', '0', 'post');
		$timesec = JRequest::getInt ( 'timesec', '0', 'post');
		$timehun = JRequest::getInt ( 'timehun', '0', 'post');

		$time = $timemin * 60 + $timesec + $timehun / 100;
		$row->time = $time;

		if (! $row->result_date)
		$row->result_date = date ( 'Y-m-d' );
		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."');
				window.history.go(-1); </script>\n"; 
			exit();
		}
		switch ( $this->_task) {
			case 'apply' :
				$msg = 'Changes to Times saved';
				$link = 'index.php?option=' . $option . '&task=edit&cid[]=' . $row->id;
				break;
			case 'save' :
			default :
				$msg = 'Times Saved';
				$link = 'index.php?option=' . $option;
				break;
		}
		$this->setRedirect ( $link, $msg );
	}

	function prikaziUtakmice() {
		global $option, $mainframe;
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
		$limitstart = JRequest::getVar('limitstart', 0);
		$db = & JFactory::getDBO ();
		//		$query1 = "SELECT count(*) FROM #__fsno_utakmice";
		//		$db->setQuery( $query1 );
		//		$total = $db->loadResult();
		$query = "SELECT u.id,
				  FROM #__fsno_utakmice AS u";
		$db->setQuery ( $query, $limitstart, $limit  );
		$rows = $db->loadObjectList ();
		if ($db->getErrorNum ()) {
			echo $db->stderr ();
			return false;
		}
		$total = count($rows);
		jimport('joomla.html.pagination');
		$pageNav = new JPagination($total, $limitstart, $limit);
		HTML_fudbal::prikaziUtakmice ( $option, $rows, $pageNav  );
	}

	function removeTimes() {
		global $option;
		$cid = JRequest::getVar ( 'cid', array ( ), '', 'array' );
		$db = & JFactory::getDBO ();
		if (count ( $cid )) {
			$cids = implode ( ',', $cid );
			$query = "DELETE FROM #__st_times WHERE id IN ( $cids )";
			$db->setQuery ( $query );
			if (! $db->query ()) {
				echo "<script> alert('" . $db->getErrorMsg () . "');
				window.history.go(-1); </script>\n";
			}
		}
		$this->setRedirect ( 'index.php?option=' . $option );
	}

	function prikaziKola() {
		global $option, $mainframe;
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
		$limitstart = JRequest::getVar('limitstart', 0);
		$db =& JFactory::getDBO();
		$query = "SELECT count(*) FROM #__fsno_kola";
		$db->setQuery( $query );
		$total = $db->loadResult();
		$query = "SELECT k.id, k.ime, k.pocetak, k.kraj, s.ime AS sezona FROM #__fsno_kolo AS k, #__fsno_sezona AS s WHERE k.sezona_id = s.id ";
		$db->setQuery( $query, $limitstart, $limit );
		$rows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		jimport('joomla.html.pagination');
		$pageNav = new JPagination($total, $limitstart, $limit);
		HTML_fudbal::prikaziKola( $option, $rows, $pageNav );
	}

	function editujKolo()
	{
		global $option;
		$row =& JTable::getInstance('kolo', 'Table');
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$id = $cid[0];
		$row->load($id);

		$lists = array ( );
		$db = & JFactory::getDBO ();
		$query = "SELECT id AS sezona_id, ime FROM #__fsno_sezona ORDER BY ime ASC";
		$db->setQuery ( $query );
		$rowsSezone = $db->loadObjectList ();

		$lists ['sezone'] = JHTML::_ ( 'select.genericList', $rowsSezone, 'sezona_id', 'class="inputbox" ' . '', 'sezona_id', 'ime', $row->sezona_id );
		HTML_fudbal::editujKolo($row, $lists, $option);
	}

	function sacuvajKolo()
	{
		global $option;
		$row =& JTable::getInstance('kolo', 'Table');
		if (!$row->bind(JRequest::get('post')))
		{
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		switch ( $this->_task) {
			case 'primeniIzmeneKola' :
				$msg = JText::_( 'KOLO_APPLY' );
				$link = 'index.php?option=' . $option . '&task=editujKolo&cid[]=' . $row->id;
				break;
			case 'sacuvajKolo' :
			default :
				$msg = JText::_( 'KOLO_SAVE' );
				$link = 'index.php?option=' . $option . '&task=kolo';
				break;
		}
		$this->setRedirect( $link, $msg );
	}

	function prikaziLige() {
		global $option, $mainframe;
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
		$limitstart = JRequest::getVar('limitstart', 0);
		$db =& JFactory::getDBO();
		$query = "SELECT count(*) FROM #__fsno_lige";
		$db->setQuery( $query );
		$total = $db->loadResult();
		$query = "SELECT l.id, l.ime, l.rukovodstvo_clan_1, l.rukovodstvo_clan_2, l.rukovodstvo_clan_3, l.aktivna AS published, tl.ime AS tip_lige FROM #__fsno_lige AS l, #__fsno_tip_lige AS tl WHERE l.tip_id = tl.id ";
		$db->setQuery( $query, $limitstart, $limit );
		$rows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		jimport('joomla.html.pagination');
		$pageNav = new JPagination($total, $limitstart, $limit);
		HTML_fudbal::prikaziLige( $option, $rows, $pageNav );
	}

	function editujLigu()
	{
		global $option;
		$row =& JTable::getInstance('lige', 'Table');
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$id = $cid[0];
		$row->load($id);

		$lists = array ( );
		$db = & JFactory::getDBO ();
		$query = "SELECT id AS tip_id, ime FROM #__fsno_tip_lige ORDER BY ime ASC";
		$db->setQuery ( $query );
		$rowsTipLige = $db->loadObjectList ();

		$lists ['tip_lige'] = JHTML::_ ( 'select.genericList', $rowsTipLige, 'tip_id', 'class="inputbox" ' . '', 'tip_id', 'ime', $row->tip_id );
		$lists['aktivna'] = JHTML::_('select.booleanlist', 'aktivna', 'class="inputbox"', $row->aktivna);
		HTML_fudbal::editujLigu($row, $lists, $option);
	}

	function sacuvajLigu()
	{
		global $option;
		$row =& JTable::getInstance('lige', 'Table');
		if (!$row->bind(JRequest::get('post')))
		{
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		switch ( $this->_task ) {
			case 'primeniIzmeneLige' :
				$msg = JText::_('LIGA_APPLY');
				$link = 'index.php?option=' . $option . '&task=editujLigu&cid[]=' . $row->id;
				break;
			case 'sacuvajLigu' :
			default :
				$msg = JText::_('LIGA_SAVE');
				$link = 'index.php?option=' . $option . '&task=lige';
				break;
		}
		$this->setRedirect( $link, $msg );
	}

	function ligapublish()
	{
		global $option;
		$cid = JRequest::getVar('cid', array(), '', 'array');
		$totalSelected	= count($cid);
		$totalChanged = 0;
		if ($this->_task == 'ligapublish') {
			$publish = 1;
		}
		else {
			$publish = 0;
		}

		if(is_array($cid))
		{
			$row =& JTable::getInstance('lige', 'Table');
			foreach ($cid as $id)
			{
				$row->load( $id );
				if($row->aktivna != $publish) {
					$row->aktivna = $publish;
	
					if (!$row->check()) {
						$this->setError($row->getError());
						$publish = -1;
					}
					if (!$row->store()) {
						$this->setError($row->getError());
						$publish = -1;
					}
					$totalChanged++;
				}	
			}
		}
		$totalNotChanged = $totalSelected - $totalChanged;
		switch ($publish)
		{
			case 1 :
				$msg = JText::sprintf(JText::_('LIGE_AKTIVIRANE'), $totalChanged, $totalSelected, $totalNotChanged);
				break;

			case 0 :
				$msg = JText::sprintf(JText::_('LIGE_DEAKTIVIRANE'), $totalChanged, $totalSelected, $totalNotChanged);
				break;
				
			default :
				$msg = $this->getError();
		}
		$this->setRedirect('index.php?option=' . $option .'&task=lige', $msg);
	}
	
function prikaziTrenere() {
		global $option, $mainframe;
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
		$limitstart = JRequest::getVar('limitstart', 0);
		$db =& JFactory::getDBO();
		$query = "SELECT count(*) FROM #__fsno_treneri";
		$db->setQuery( $query );
		$total = $db->loadResult();
		$query = "SELECT t.id, t.ime, t.prezime, t.aktivan AS published, l.ime AS licenca FROM #__fsno_treneri AS t, #__fsno_licence AS l WHERE t.licenca_id = l.id ";
		$db->setQuery( $query, $limitstart, $limit );
		$rows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		jimport('joomla.html.pagination');
		$pageNav = new JPagination($total, $limitstart, $limit);
		HTML_fudbal::prikaziTrenere( $option, $rows, $pageNav );
	}

	function editujTrenera()
	{
		global $option;
		$row =& JTable::getInstance('treneri', 'Table');
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$id = $cid[0];
		$row->load($id);

		$lists = array ( );
		$db = & JFactory::getDBO ();
		$query = "SELECT id AS licenca_id, ime FROM #__fsno_licence ORDER BY ime ASC";
		$db->setQuery ( $query );
		$rowsTipLicence = $db->loadObjectList ();

		$lists ['licenca'] = JHTML::_ ( 'select.genericList', $rowsTipLicence, 'licenca_id', 'class="inputbox" ' . '', 'licenca_id', 'ime', $row->licenca_id );
		$lists['aktivan'] = JHTML::_('select.booleanlist', 'aktivan', 'class="inputbox"', $row->aktivan);
		HTML_fudbal::editujTrenera($row, $lists, $option);
	}

	function sacuvajTrenera()
	{
		global $option;
		$row =& JTable::getInstance('treneri', 'Table');
		if (!$row->bind(JRequest::get('post')))
		{
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		switch ( $this->_task ) {
			case 'primeniIzmeneTrenera' :
				$msg = JText::_('TRENER_APPLY');
				$link = 'index.php?option=' . $option . '&task=editujTrenera&cid[]=' . $row->id;
				break;
			case 'sacuvajTrenera' :
			default :
				$msg = JText::_('TRENER_SAVE');
				$link = 'index.php?option=' . $option . '&task=treneri';
				break;
		}
		$this->setRedirect( $link, $msg );
	}

	function trenerpublish()
	{
		global $option;
		$cid = JRequest::getVar('cid', array(), '', 'array');
		$totalSelected	= count($cid);
		$totalChanged = 0;
		if ($this->_task == 'trenerpublish') {
			$publish = 1;
		}
		else {
			$publish = 0;
		}

		if(is_array($cid))
		{
			$row =& JTable::getInstance('treneri', 'Table');
			foreach ($cid as $id)
			{
				$row->load( $id );
				if($row->aktivan != $publish) {
					$row->aktivan = $publish;
	
					if (!$row->check()) {
						$this->setError($row->getError());
						$publish = -1;
					}
					if (!$row->store()) {
						$this->setError($row->getError());
						$publish = -1;
					}
					$totalChanged++;
				}	
			}
		}
		$totalNotChanged = $totalSelected - $totalChanged;
		switch ($publish)
		{
			case 1 :
				$msg = JText::sprintf(JText::_('TRENERI_AKTIVIRANI'), $totalChanged, $totalSelected, $totalNotChanged);
				break;

			case 0 :
				$msg = JText::sprintf(JText::_('TRENERI_DEAKTIVIRANI'), $totalChanged, $totalSelected, $totalNotChanged);
				break;
				
			default :
				$msg = $this->getError();
		}
		$this->setRedirect('index.php?option=' . $option .'&task=treneri', $msg);
	}
	
function prikaziKlubove() {
		global $option, $mainframe;
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
		$limitstart = JRequest::getVar('limitstart', 0);
		$db =& JFactory::getDBO();
		$query = "SELECT count(*) FROM #__fsno_klubovi";
		$db->setQuery( $query );
		$total = $db->loadResult();
		$query = "SELECT k.id, k.ime, k.mesto, k.predsednik, k.kontakt_tel, k.sekretar, 
				 k.osnovna_boja_dresa, k.rezervna_boja_dresa, k.naziv_igralista, 
				 k.mesto_igralista, k.ziro_racun, k.aktivan AS published, l.ime AS liga, t.ime AS trener_ime, t.prezime AS trener_prezime 
				 FROM #__fsno_klubovi AS k, #__fsno_lige AS l, #__fsno_treneri AS t 
				 WHERE k.liga_id = l.id AND k.trener_id = t.id ";
		$db->setQuery( $query, $limitstart, $limit );
		$rows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		jimport('joomla.html.pagination');
		$pageNav = new JPagination($total, $limitstart, $limit);
		HTML_fudbal::prikaziKlubove( $option, $rows, $pageNav );
	}

	function editujKlub()
	{
		global $option;
		$row =& JTable::getInstance('treneri', 'Table');
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$id = $cid[0];
		$row->load($id);

		$lists = array ( );
		$db = & JFactory::getDBO ();
		$query = "SELECT id AS licenca_id, ime FROM #__fsno_licence ORDER BY ime ASC";
		$db->setQuery ( $query );
		$rowsTipLicence = $db->loadObjectList ();

		$lists ['licenca'] = JHTML::_ ( 'select.genericList', $rowsTipLicence, 'licenca_id', 'class="inputbox" ' . '', 'licenca_id', 'ime', $row->licenca_id );
		$lists['aktivan'] = JHTML::_('select.booleanlist', 'aktivan', 'class="inputbox"', $row->aktivan);
		HTML_fudbal::editujTrenera($row, $lists, $option);
	}

	function sacuvajKlub()
	{
		global $option;
		$row =& JTable::getInstance('treneri', 'Table');
		if (!$row->bind(JRequest::get('post')))
		{
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		switch ( $this->_task ) {
			case 'primeniIzmeneTrenera' :
				$msg = JText::_('TRENER_APPLY');
				$link = 'index.php?option=' . $option . '&task=editujTrenera&cid[]=' . $row->id;
				break;
			case 'sacuvajTrenera' :
			default :
				$msg = JText::_('TRENER_SAVE');
				$link = 'index.php?option=' . $option . '&task=treneri';
				break;
		}
		$this->setRedirect( $link, $msg );
	}

	function klubpublish()
	{
		global $option;
		$cid = JRequest::getVar('cid', array(), '', 'array');
		$totalSelected	= count($cid);
		$totalChanged = 0;
		if ($this->_task == 'trenerpublish') {
			$publish = 1;
		}
		else {
			$publish = 0;
		}

		if(is_array($cid))
		{
			$row =& JTable::getInstance('treneri', 'Table');
			foreach ($cid as $id)
			{
				$row->load( $id );
				if($row->aktivan != $publish) {
					$row->aktivan = $publish;
	
					if (!$row->check()) {
						$this->setError($row->getError());
						$publish = -1;
					}
					if (!$row->store()) {
						$this->setError($row->getError());
						$publish = -1;
					}
					$totalChanged++;
				}	
			}
		}
		$totalNotChanged = $totalSelected - $totalChanged;
		switch ($publish)
		{
			case 1 :
				$msg = JText::sprintf(JText::_('TRENERI_AKTIVIRANI'), $totalChanged, $totalSelected, $totalNotChanged);
				break;

			case 0 :
				$msg = JText::sprintf(JText::_('TRENERI_DEAKTIVIRANI'), $totalChanged, $totalSelected, $totalNotChanged);
				break;
				
			default :
				$msg = $this->getError();
		}
		$this->setRedirect('index.php?option=' . $option .'&task=treneri', $msg);
	}
	
	function removeMeet()
	{
		global $option;
		$cid = JRequest::getVar( 'cid', array(), '', 'array' );
		$db =& JFactory::getDBO();
		if(count($cid))
		{
			$cids = implode( ',', $cid );
			$query = "DELETE FROM #__st_meets WHERE id IN ( $cids )";
			$db->setQuery( $query );
			if (!$db->query())
			{
				echo "<script> alert('".$db->getErrorMsg()."');
				window.history.go(-1);
				</script>\n";
			}
		}
		$this->setRedirect( 'index.php?option=' . $option . '&task=meets');
	}

	function showMtimes() {
		global $option, $mainframe;
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
		$limitstart = JRequest::getVar('limitstart', 0);
		$db =& JFactory::getDBO();
		$query = "SELECT count(*) FROM #__st_motivational_times";
		$db->setQuery( $query );
		$total = $db->loadResult();
		$query = "SELECT m.id, m.gender, m.time, c.short_name AS course, c.long_name, s.short_name AS stroke, d.distance, d.unit, a.name AS age_group, l.name AS level
				  FROM #__st_motivational_times AS m, #__st_course_type AS c, #__st_stroke AS s, #__st_distance AS d, #__st_age_group AS a, #__st_levels AS l 
				  WHERE m.course_type_id = c.id AND m.stroke_id = s.id AND m.distance_id = d.id AND m.age_group_id = a.id AND m.time_levels_id = l.id 
				  ORDER BY a.id ASC, l.id ASC, m.gender ASC, c.id ASC";
		$db->setQuery( $query, $limitstart, $limit );
		$rows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}

		for($i = 0; $i < count ( $rows ); $i ++) {
			$seconds = floor($rows[$i]->time);
			$hundreds = ($rows[$i]->time - $seconds)*100;
			if($hundreds < 10){
				$hundreds = '0'.$hundreds;
			}
			if($seconds >= 60){
				$minutes = floor($seconds / 60);
				$seconds = $seconds % 60;
				if($seconds < 10){
					$seconds = '0'.$seconds;
				}
				$rows[$i]->time = $minutes . ':' . $seconds . '.' . $hundreds;
			}
		}
		jimport('joomla.html.pagination');
		$pageNav = new JPagination($total, $limitstart, $limit);
		HTML_reviews::showMtimes( $option, $rows, $pageNav );
	}

	function editMtimes()
	{
		global $option;
		$row = & JTable::getInstance ( 'Mtimes', 'Table' );
		$cid = JRequest::getVar ( 'cid', array (0 ), '', 'array' );
		$db = & JFactory::getDBO ();
		$id = $cid [0];
		$row->load ( $id );
		//converting float data time in standard format
		$seconds = floor($row->time);
		$hundreds = ($row->time - $seconds)*100;
		if($hundreds < 10){
			$hundreds = '0'.$hundreds;
		}
		if($seconds < 10){
			$seconds = '0'.$seconds;
		}
		if($seconds >= 60){
			$minutes = floor($seconds / 60);
			$seconds = $seconds % 60;
				
			$row->minutes = $minutes ;
			$row->seconds = $seconds ;
			$row->hundreds = $hundreds ;
		}else{
			$row->minutes = 0 ;
			$row->seconds = $seconds ;
			$row->hundreds = $hundreds ;
		}

		$lists = array ( );

		$query = "SELECT id, name FROM #__st_age_group ORDER BY id ASC";
		$db->setQuery ( $query );
		$rowsAgeGroup = $db->loadObjectList ();

		$query3 = "SELECT id, short_name, long_name FROM #__st_course_type ORDER BY long_name ASC";
		$db->setQuery ( $query3 );
		$rowsCourse = $db->loadObjectList ();
		for($i = 0; $i < count ( $rowsCourse ); $i ++) {
			$rowsCourse [$i]->long_name = $rowsCourse [$i]->long_name . ' (' . $rowsCourse [$i]->short_name . ')';
		}

		$query4 = "SELECT id, long_name FROM #__st_stroke ORDER BY long_name ASC";
		$db->setQuery ( $query4 );
		$rowsStroke = $db->loadObjectList ();

		$query5 = "SELECT id, distance, unit FROM #__st_distance ORDER BY unit ASC, distance ASC";
		$db->setQuery ( $query5 );
		$rowsDistance = $db->loadObjectList ();
		for($i = 0; $i < count ( $rowsDistance ); $i ++) {
			$rowsDistance [$i]->distance = $rowsDistance [$i]->distance . ' ' . $rowsDistance [$i]->unit;
		}

		$query6 = "SELECT id, name FROM #__st_levels ORDER BY id ASC";
		$db->setQuery ( $query6 );
		$rowsTimeLevels = $db->loadObjectList ();

		$gender = array(
			'0' => array('value' => 'Male',
			'text' => 'Boys'),
			'1' => array('value' => 'Female',
			'text' => 'Girls')
		);

		$lists['gender'] = JHTML::_('select.genericList', $gender, 'gender', 'class="inputbox" '. '', 'value', 'text', $row->gender );
		$lists ['agegroups'] = JHTML::_ ( 'select.genericList', $rowsAgeGroup, 'age_group_id', 'class="inputbox" ' . '', 'id', 'name', $row->age_group_id );
		$lists ['courses'] = JHTML::_ ( 'select.genericList', $rowsCourse, 'course_type_id', 'class="inputbox" ' . '', 'id', 'long_name', $row->course_type_id );
		$lists ['strokes'] = JHTML::_ ( 'select.genericList', $rowsStroke, 'stroke_id', 'class="inputbox" ' . '', 'id', 'long_name', $row->stroke_id );
		$lists ['distances'] = JHTML::_ ( 'select.genericList', $rowsDistance, 'distance_id', 'class="inputbox" ' . '', 'id', 'distance', $row->distance_id );
		$lists ['timelevels'] = JHTML::_ ( 'select.genericList', $rowsTimeLevels, 'time_levels_id', 'class="inputbox" ' . '', 'id', 'name', $row->time_levels_id );

		HTML_reviews::editMtimes ( $row, $lists, $option );
	}

	function saveMtimes()
	{
		global $option;
		$row =& JTable::getInstance('mtimes', 'Table');
		if (!$row->bind(JRequest::get('post')))
		{
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}

		$timemin = JRequest::getInt ( 'timemin', '0', 'post');
		$timesec = JRequest::getInt ( 'timesec', '0', 'post');
		$timehun = JRequest::getInt ( 'timehun', '0', 'post');

		$time = $timemin * 60 + $timesec + $timehun / 100;
		$row->time = $time;

		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		switch ( $this->_task) {
			case 'apply' :
				$msg = 'Changes to Motivational Time Saved';
				$link = 'index.php?option=' . $option . '&task=editMtimes&cid[]=' . $row->id;
				break;
			case 'save' :
			default :
				$msg = 'Motivational Time Saved';
				$link = 'index.php?option=' . $option . '&task=motiv_times';
				break;
		}
		$this->setRedirect( $link, $msg );
	}

	function removeMtimes()
	{
		global $option;
		$cid = JRequest::getVar( 'cid', array(), '', 'array' );
		$db =& JFactory::getDBO();
		if(count($cid))
		{
			$cids = implode( ',', $cid );
			$query = "DELETE FROM #__st_motivational_times WHERE id IN ( $cids )";
			$db->setQuery( $query );
			if (!$db->query())
			{
				echo "<script> alert('".$db->getErrorMsg()."');
				window.history.go(-1);
				</script>\n";
			}
		}
		$this->setRedirect( 'index.php?option=' . $option . '&task=motiv_times');
	}

	function showMptimes() {
		global $option, $mainframe;
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
		$limitstart = JRequest::getVar('limitstart', 0);
		$db =& JFactory::getDBO();
		$query = "SELECT count(*) FROM #__st_motivational_times_percentile";
		$db->setQuery( $query );
		$total = $db->loadResult();
		$query = "SELECT m.id, m.gender, m.time, c.short_name AS course, c.long_name, s.short_name AS stroke, d.distance, d.unit, a.name AS age_group, t.name AS track, t.percent
				  FROM #__st_motivational_times_percentile AS m, #__st_course_type AS c, #__st_stroke AS s, #__st_distance AS d, #__st_age_group AS a, #__st_track AS t 
				  WHERE m.course_type_id = c.id AND m.stroke_id = s.id AND m.distance_id = d.id AND m.age_group_id = a.id AND m.track_id = t.id 
				  ORDER BY a.id ASC, m.gender ASC, c.id ASC, d.distance ASC, t.id ASC";
		$db->setQuery( $query, $limitstart, $limit );
		$rows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}

		for($i = 0; $i < count ( $rows ); $i ++) {
			$seconds = floor($rows[$i]->time);
			$hundreds = ($rows[$i]->time - $seconds)*100;
			if($hundreds < 10){
				$hundreds = '0'.$hundreds;
			}
			if($seconds >= 60){
				$minutes = floor($seconds / 60);
				$seconds = $seconds % 60;
				if($seconds < 10){
					$seconds = '0'.$seconds;
				}
				$rows[$i]->time = $minutes . ':' . $seconds . '.' . $hundreds;
			}
		}
		jimport('joomla.html.pagination');
		$pageNav = new JPagination($total, $limitstart, $limit);
		HTML_reviews::showMptimes( $option, $rows, $pageNav );
	}

	function editMptimes()
	{
		global $option;
		$row = & JTable::getInstance ( 'Mptimes', 'Table' );
		$cid = JRequest::getVar ( 'cid', array (0 ), '', 'array' );
		$db = & JFactory::getDBO ();
		$id = $cid [0];
		$row->load ( $id );
		//converting float data time in standard format
		$seconds = floor($row->time);
		$hundreds = ($row->time - $seconds)*100;
		if($hundreds < 10){
			$hundreds = '0'.$hundreds;
		}
		if($seconds < 10){
			$seconds = '0'.$seconds;
		}
		if($seconds >= 60){
			$minutes = floor($seconds / 60);
			$seconds = $seconds % 60;
				
			$row->minutes = $minutes ;
			$row->seconds = $seconds ;
			$row->hundreds = $hundreds ;
		}else{
			$row->minutes = 0 ;
			$row->seconds = $seconds ;
			$row->hundreds = $hundreds ;
		}

		$lists = array ( );

		$query = "SELECT id, name FROM #__st_age_group ORDER BY id ASC";
		$db->setQuery ( $query );
		$rowsAgeGroup = $db->loadObjectList ();

		$query3 = "SELECT id, short_name, long_name FROM #__st_course_type ORDER BY long_name ASC";
		$db->setQuery ( $query3 );
		$rowsCourse = $db->loadObjectList ();
		for($i = 0; $i < count ( $rowsCourse ); $i ++) {
			$rowsCourse [$i]->long_name = $rowsCourse [$i]->long_name . ' (' . $rowsCourse [$i]->short_name . ')';
		}

		$query4 = "SELECT id, long_name FROM #__st_stroke ORDER BY long_name ASC";
		$db->setQuery ( $query4 );
		$rowsStroke = $db->loadObjectList ();

		$query5 = "SELECT id, distance, unit FROM #__st_distance ORDER BY unit ASC, distance ASC";
		$db->setQuery ( $query5 );
		$rowsDistance = $db->loadObjectList ();
		for($i = 0; $i < count ( $rowsDistance ); $i ++) {
			$rowsDistance [$i]->distance = $rowsDistance [$i]->distance . ' ' . $rowsDistance [$i]->unit;
		}

		$query6 = "SELECT id, name, percent FROM #__st_track ORDER BY id ASC";
		$db->setQuery ( $query6 );
		$rowsTimeTracks = $db->loadObjectList ();
		for($i = 0; $i < count ( $rowsTimeTracks ); $i ++) {
			$rowsTimeTracks [$i]->name = $rowsTimeTracks [$i]->name . ' - ' . $rowsTimeTracks [$i]->percent . ' %';
		}

		$gender = array(
			'0' => array('value' => 'Male',
			'text' => 'Boys'),
			'1' => array('value' => 'Female',
			'text' => 'Girls')
		);

		$lists['gender'] = JHTML::_('select.genericList', $gender, 'gender', 'class="inputbox" '. '', 'value', 'text', $row->gender );
		$lists ['agegroups'] = JHTML::_ ( 'select.genericList', $rowsAgeGroup, 'age_group_id', 'class="inputbox" ' . '', 'id', 'name', $row->age_group_id );
		$lists ['courses'] = JHTML::_ ( 'select.genericList', $rowsCourse, 'course_type_id', 'class="inputbox" ' . '', 'id', 'long_name', $row->course_type_id );
		$lists ['strokes'] = JHTML::_ ( 'select.genericList', $rowsStroke, 'stroke_id', 'class="inputbox" ' . '', 'id', 'long_name', $row->stroke_id );
		$lists ['distances'] = JHTML::_ ( 'select.genericList', $rowsDistance, 'distance_id', 'class="inputbox" ' . '', 'id', 'distance', $row->distance_id );
		$lists ['timetracks'] = JHTML::_ ( 'select.genericList', $rowsTimeTracks, 'track_id', 'class="inputbox" ' . '', 'id', 'name', $row->track_id );

		HTML_reviews::editMptimes ( $row, $lists, $option );
	}

	function saveMptimes()
	{
		global $option;
		$row =& JTable::getInstance('mptimes', 'Table');
		if (!$row->bind(JRequest::get('post')))
		{
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}

		$timemin = JRequest::getInt ( 'timemin', '0', 'post');
		$timesec = JRequest::getInt ( 'timesec', '0', 'post');
		$timehun = JRequest::getInt ( 'timehun', '0', 'post');

		$time = $timemin * 60 + $timesec + $timehun / 100;
		$row->time = $time;

		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		switch ( $this->_task) {
			case 'apply' :
				$msg = 'Changes to Motivational Percentile Time Saved';
				$link = 'index.php?option=' . $option . '&task=editMptimes&cid[]=' . $row->id;
				break;
			case 'save' :
			default :
				$msg = 'Motivational Percentile Time Saved';
				$link = 'index.php?option=' . $option . '&task=motivp_times';
				break;
		}
		$this->setRedirect( $link, $msg );
	}

	function removeMptimes()
	{
		global $option;
		$cid = JRequest::getVar( 'cid', array(), '', 'array' );
		$db =& JFactory::getDBO();
		if(count($cid))
		{
			$cids = implode( ',', $cid );
			$query = "DELETE FROM #__st_motivational_times_percentile WHERE id IN ( $cids )";
			$db->setQuery( $query );
			if (!$db->query())
			{
				echo "<script> alert('".$db->getErrorMsg()."');
				window.history.go(-1);
				</script>\n";
			}
		}
		$this->setRedirect( 'index.php?option=' . $option . '&task=motivp_times');
	}

	function showQtimes() {
		global $option, $mainframe;
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
		$limitstart = JRequest::getVar('limitstart', 0);
		$db =& JFactory::getDBO();
		$query = "SELECT count(*) FROM #__st_qualifying_meet_times";
		$db->setQuery( $query );
		$total = $db->loadResult();
		$query = "SELECT q.id, q.gender, q.time, c.short_name AS course, c.long_name, s.short_name AS stroke, d.distance, d.unit, a.name AS age_group, m.name AS meet
				  FROM #__st_qualifying_meet_times AS q, #__st_course_type AS c, #__st_stroke AS s, #__st_distance AS d, #__st_age_group AS a, #__st_meets AS m 
				  WHERE q.course_type_id = c.id AND q.stroke_id = s.id AND q.distance_id = d.id AND q.age_group_id = a.id AND q.meet_id = m.id 
				  ORDER BY m.name ASC, a.id ASC, q.gender ASC, c.id ASC, d.distance ASC";
		$db->setQuery( $query, $limitstart, $limit );
		$rows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}

		for($i = 0; $i < count ( $rows ); $i ++) {
			$seconds = floor($rows[$i]->time);
			$hundreds = ($rows[$i]->time - $seconds)*100;
			if($hundreds < 10){
				$hundreds = '0'.$hundreds;
			}
			if($seconds >= 60){
				$minutes = floor($seconds / 60);
				$seconds = $seconds % 60;
				if($seconds < 10){
					$seconds = '0'.$seconds;
				}
				$rows[$i]->time = $minutes . ':' . $seconds . '.' . $hundreds;
			}
		}
		jimport('joomla.html.pagination');
		$pageNav = new JPagination($total, $limitstart, $limit);
		HTML_reviews::showQtimes( $option, $rows, $pageNav );
	}

	function editQtimes()
	{
		global $option;
		$row = & JTable::getInstance ( 'Qtimes', 'Table' );
		$cid = JRequest::getVar ( 'cid', array (0 ), '', 'array' );
		$db = & JFactory::getDBO ();
		$id = $cid [0];
		$row->load ( $id );
		//converting float data time in standard format
		$seconds = floor($row->time);
		$hundreds = ($row->time - $seconds)*100;
		if($hundreds < 10){
			$hundreds = '0'.$hundreds;
		}
		if($seconds < 10){
			$seconds = '0'.$seconds;
		}
		if($seconds >= 60){
			$minutes = floor($seconds / 60);
			$seconds = $seconds % 60;
				
			$row->minutes = $minutes ;
			$row->seconds = $seconds ;
			$row->hundreds = $hundreds ;
		}else{
			$row->minutes = 0 ;
			$row->seconds = $seconds ;
			$row->hundreds = $hundreds ;
		}

		$lists = array ( );

		$query = "SELECT id, name FROM #__st_age_group ORDER BY id ASC";
		$db->setQuery ( $query );
		$rowsAgeGroup = $db->loadObjectList ();

		$query3 = "SELECT id, short_name, long_name FROM #__st_course_type ORDER BY long_name ASC";
		$db->setQuery ( $query3 );
		$rowsCourse = $db->loadObjectList ();
		for($i = 0; $i < count ( $rowsCourse ); $i ++) {
			$rowsCourse [$i]->long_name = $rowsCourse [$i]->long_name . ' (' . $rowsCourse [$i]->short_name . ')';
		}

		$query4 = "SELECT id, long_name FROM #__st_stroke ORDER BY long_name ASC";
		$db->setQuery ( $query4 );
		$rowsStroke = $db->loadObjectList ();

		$query5 = "SELECT id, distance, unit FROM #__st_distance ORDER BY unit ASC, distance ASC";
		$db->setQuery ( $query5 );
		$rowsDistance = $db->loadObjectList ();
		for($i = 0; $i < count ( $rowsDistance ); $i ++) {
			$rowsDistance [$i]->distance = $rowsDistance [$i]->distance . ' ' . $rowsDistance [$i]->unit;
		}

		$query6 = "SELECT id, name FROM #__st_meets ORDER BY name ASC";
		$db->setQuery ( $query6 );
		$rowsMeets = $db->loadObjectList ();

		$gender = array(
			'0' => array('value' => 'Male',
			'text' => 'Boys'),
			'1' => array('value' => 'Female',
			'text' => 'Girls')
		);

		$lists['gender'] = JHTML::_('select.genericList', $gender, 'gender', 'class="inputbox" '. '', 'value', 'text', $row->gender );
		$lists ['agegroups'] = JHTML::_ ( 'select.genericList', $rowsAgeGroup, 'age_group_id', 'class="inputbox" ' . '', 'id', 'name', $row->age_group_id );
		$lists ['courses'] = JHTML::_ ( 'select.genericList', $rowsCourse, 'course_type_id', 'class="inputbox" ' . '', 'id', 'long_name', $row->course_type_id );
		$lists ['strokes'] = JHTML::_ ( 'select.genericList', $rowsStroke, 'stroke_id', 'class="inputbox" ' . '', 'id', 'long_name', $row->stroke_id );
		$lists ['distances'] = JHTML::_ ( 'select.genericList', $rowsDistance, 'distance_id', 'class="inputbox" ' . '', 'id', 'distance', $row->distance_id );
		$lists ['meets'] = JHTML::_ ( 'select.genericList', $rowsMeets, 'meet_id', 'class="inputbox" ' . '', 'id', 'name', $row->meet_id );

		HTML_reviews::editQtimes ( $row, $lists, $option );
	}

	function saveQtimes()
	{
		global $option;
		$row =& JTable::getInstance('qtimes', 'Table');
		if (!$row->bind(JRequest::get('post')))
		{
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}

		$timemin = JRequest::getInt ( 'timemin', '0', 'post');
		$timesec = JRequest::getInt ( 'timesec', '0', 'post');
		$timehun = JRequest::getInt ( 'timehun', '0', 'post');

		$time = $timemin * 60 + $timesec + $timehun / 100;
		$row->time = $time;

		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."');
			window.history.go(-1);
			</script>\n";
			exit();
		}
		switch ( $this->_task) {
			case 'apply' :
				$msg = 'Changes to Qualifying Time Saved';
				$link = 'index.php?option=' . $option . '&task=editQtimes&cid[]=' . $row->id;
				break;
			case 'save' :
			default :
				$msg = 'Qualifying Time Saved';
				$link = 'index.php?option=' . $option . '&task=qf_times';
				break;
		}
		$this->setRedirect( $link, $msg );
	}

	function removeQtimes()
	{
		global $option;
		$cid = JRequest::getVar( 'cid', array(), '', 'array' );
		$db =& JFactory::getDBO();
		if(count($cid))
		{
			$cids = implode( ',', $cid );
			$query = "DELETE FROM #__st_qualifying_meet_times WHERE id IN ( $cids )";
			$db->setQuery( $query );
			if (!$db->query())
			{
				echo "<script> alert('".$db->getErrorMsg()."');
				window.history.go(-1);
				</script>\n";
			}
		}
		$this->setRedirect( 'index.php?option=' . $option . '&task=qf_times');
	}
}
?>